package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.library.widget.HSSipEditText;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.datahandler.ForgetPasswordResetPwdHandler;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;


/**
 * @version V1.0
 * @Title: ForgetPasswordValidateActivity.java
 * @Description: 忘记密码-设置新密码
 * @date 2015-12-3 上午9:54:59
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class ForgetPasswordResetPwdActivity extends BaseActivity implements OnClickListener {
    private final static String TAG = "ForgetPasswordResetPwdActivity";
    /**
     * 保存sharedprefreence手机号
     */
    private static final String FORGETPASS_PHONE = "FORGETPASSPHONE";

    private ValidateButton btn_getvalidate;
    /**
     *
     */
    private HSSharedPreferences shared;

    private TextView tv_msg;
    /**
     * 提交按钮
     */
    private HSButton btn_submit;
    private HSEditText et_validate;
    private HSSipEditText et_password;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 验证码
     */
    private String validate;
    /**
     * 密码
     */
    private String password;


    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        phone = extras.getString(Extras.PHONE);

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_forgetpass_step2_reset;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        shared = new HSSharedPreferences(this, SPPath.file_configInfo);
        setToolbarTitle(R.string.set_newpass);
        btn_getvalidate.init(TAG);
        String savedphone = shared.getShareProf(FORGETPASS_PHONE);
        //保存在后台的手机号
        if (!TextUtils.isEmpty(savedphone) && !TextUtils.equals(savedphone, phone)) {
            btn_getvalidate.reset();
            shared.putShareProf(FORGETPASS_PHONE, "");
        }
        btn_getvalidate.onClick(btn_getvalidate);
        btn_getvalidate.setOnClickListener(this);
        btn_submit.setOnClickListener(this);

        mDataHandler = new ForgetPasswordResetPwdHandler(mContext);
        showText();
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    private void findView() {
        btn_getvalidate = findView(R.id.forgetpass_btn_getvalidate);
        btn_submit = findView(R.id.btn_forgetpass_confirm);
        tv_msg = findView(R.id.forgetpass_tv_msg);
        et_validate = findView(R.id.forgetpass_et_validate);
        et_password = findView(R.id.et_forgetpass_pass);
    }

    @Override
    protected void onNetworkConnected(NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected void onDestroy() {
        shared.putShareProf(FORGETPASS_PHONE, phone);
        btn_getvalidate.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (isFastDoubleClick()){
            return;
        }
        switch (v.getId()) {
            case R.id.forgetpass_btn_getvalidate://多传一个空值用于与ForgetPasswordInputPhoneActivity调用区分
                mDataHandler.interactiveSilent(mDataHandler, R.string.method_sendSms, phone, " ");
                break;
            case R.id.btn_forgetpass_confirm:
                if (checkPass()) {
                    //mobile 密码 password 验证码 smsCode，改密码
                    mDataHandler.interactive(mDataHandler, R.string.method_modifyPass, phone, password, validate);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 输入框校验
     *
     * @return
     */
    private boolean checkPass() {
        password = CommTool.getValue(et_password);
        validate = CommTool.getValue(et_validate);
        if (TextUtils.isEmpty(validate)) {
            showToast(R.string.validate_emptycheck);
        } else if (TextUtils.isEmpty(password)) {
            showToast(R.string.register_pass_emptycheck);
        } else if (!CommTool.checkVerificateCode(validate)) {
            showToast(R.string.validate_rightcheck);
        } else if (!CommTool.checkPwd(password)) {
            showToast(R.string.register_pass_rightcheck);
        } else {
            return true;
        }
        return false;
    }

    /**
     * 显示发送短信的提示
     */
    private void showText() {
        tv_msg.setText(mContext.getText(R.string.info_msg_prompt) + CommTool.encodPhone(phone));
    }

    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
